package ch8;

import java.io.Serializable;
public class PairTest1 {
    public static void main(String[] args) {
        String[] words = {"Python","is","a","program","language"};
        Pair<String> mm = Arraylg.minmax(words);
        System.out.println("min="+mm.getFirst());
        System.out.println("max="+mm.getSecond());
    }
}
    class Arraylg{
        public static <T> T getMiddle(T... a){
            return a[a.length / 2];
        }
        public static Pair<String> minmax(String[] a){
            if(a==null || a.length == 0){
                return null;
            }
            String min = a[0];
            String max = a[0];
            for (int i = 0; i < a.length; i++) {
                if (min.compareTo(a[i]) > 0) min = a[i];
                if (max.compareTo(a[i]) < 0) max = a[i];
            }
            return new Pair<String>(min,max);
        }

        /**
         * 取数组中最小元素,<T extends BoundingType>
         *     T应该是绑定类型的子类型（subtype）
         * @param a
         * @param <T>
         * @return
         */
        public static <T extends Comparable & Serializable> T min(T[] a){
            if(a==null || a.length == 0){
                return null;
            }
            T min = a[0];
            for (int i = 0; i < a.length; i++) {
                if (min.compareTo(a[i]) > 0) min = a[i];
            }
            return min;
        }
    }

